#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   list.py
@Time    :   2024-07-09 13:02:11
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   redis list 操作
'''

from typing import Any, Awaitable, Optional

from redis.asyncio import Redis

class List:
    def __init__(self, redis: Redis):
        # 初始化 Redis 连接
        self.redis = redis
    async def get_value(self, key: str, index: int) -> Optional[str]:
        """获取指定索引的值"""
        result = await self.redis.lindex(key, index) # type: ignore
        return result.decode('utf-8') if result else None # type: ignore
    async def set_value(self, key: str, value: str) -> Any:
        """添加值到列表"""
        return await self.redis.rpush(key, value)  # type: ignore
    async def delete_value(self, key: str, value: str) -> Any:
        """删除指定值"""
        return await self.redis.lrem(key, 1, value)  # type: ignore
    async def get_all_values(self, key: str): 
        """获取指定键的所有值"""
        return await self.redis.lrange(key, 0, -1)  # type: ignore